package org.lc.service.system.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.lc.platform.mybatis.entity.BaseEntity;
import org.lc.platform.mybatis.handler.SM4Handler;
import org.lc.platform.mybatis.manager.annotation.ManyToOne;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serial;
import java.io.Serializable;
import java.time.LocalDateTime;

@EqualsAndHashCode(callSuper = true)
@Data
@TableName(autoResultMap = true)
public class SysLogger extends BaseEntity implements Serializable {

    /**
     * 请求地址
     */
    @Schema(description = "请求地址")
    private String requestUri;

    /**
     * 请求参数（query）
     */
    @TableField(typeHandler = SM4Handler.class)
    @Schema(description = "请求参数（query）")
    private String queryParams;

    /**
     * 请求参数（body）
     */
    @TableField(typeHandler = SM4Handler.class)
    @Schema(description = "请求参数（body）")
    private String requestBody;



    /**
     * 响应码
     */
    @Schema(description = "响应码")
    private int responseCode;


    /**
     * 响应数据
     */
    @TableField(typeHandler = SM4Handler.class)
    @Schema(description = "响应数据")
    private String responseBody;


    /**
     * 请求开始时间
     */
    @Schema(description = "请求开始时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime startTime;


    /**
     * 请求结束时间
     */
    @Schema(description = "请求结束时间")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime endTime;


    /**
     * 响应时长
     */
    @Schema(description = "响应时长")
    private Long duration;

    /**
     * 请求方式（http or https）
     */
    @Schema(description = "请求方式（http or https）")
    private String scheme;

    /**
     * 请求类型
     */
    @Schema(description = "请求类型")
    private String method;

    /**
     * 请求Host
     */
    @Schema(description = "请求Host")
    private String clientHost;

    /**
     * 请求IP
     */
    @Schema(description = "请求IP")
    private String clientIp;


    /**
     * 日志分类
     */
    @Schema(description = "日志分类")
    private String category;

    /**
     * 日志操作对象
     */
    @Schema(description = "日志操作对象")
    @JsonIgnore
    @TableField(value = "operator_id")
    protected String operatorId;


    @ManyToOne(selfField = "operatorId", eager = true)
    @TableField(exist = false)
    private SysUser operator;

    @Serial
    private static final long serialVersionUID = 1L;
}
